package com.meevle.web;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Required;

public class SimpleSidebarFactory implements ISidebarFactory {

	private String cssClassnameCurrentPage;
	private String servletSuffix;
	private String cssClassnameHead;
	private Map<Object, Object> sidebarHeadMapping;
	
	public String getSidebar(Object servlet, Map<Object, Object> pages,
			String page) {
		StringBuffer buffer = new StringBuffer();
		String servletname = servlet.getClass().getSimpleName();
		String head = (sidebarHeadMapping==null?"Welcome":sidebarHeadMapping.get(servletname).toString());
			
		buffer.append("<ul>");
		buffer.append("<li class=\""+cssClassnameHead+"\">"+head+"</li>");
		
		Iterator<Entry<Object, Object>> it = pages.entrySet().iterator();
		while (it.hasNext()) {
			Entry<Object, Object> pairs = it.next();
			if (page.equals(pairs.getKey()))
				buffer.append("\n<li class=\"" + cssClassnameCurrentPage
						+ "\"><a href=\"" + servletname + servletSuffix
						+ "?page=" + pairs.getKey() + "\">" + pairs.getValue()
						+ "</a></li>");
			else
				buffer.append("\n<li><a href=\"" + servletname + servletSuffix
						+ "?page=" + pairs.getKey() + "\">" + pairs.getValue()
						+ "</a></li>");
		}
		buffer.append("\n</ul>");
		return buffer.toString();
	}
	
	public String getCssClassnameCurrentPage() {
		return cssClassnameCurrentPage;
	}
	
	@Required
	public void setCssClassnameCurrentPage(String cssClassnameCurrentPage) {
		this.cssClassnameCurrentPage = cssClassnameCurrentPage;
	}
	
	public String getServletSuffix() {
		return servletSuffix;
	}
	
	@Required
	public void setServletSuffix(String servletSuffix) {
		this.servletSuffix = (servletSuffix==null || servletSuffix.trim().equals("")?"":servletSuffix);
	}
	
	public String getCssClassnameHead() {
		return cssClassnameHead;
	}
	
	public void setCssClassnameHead(String cssClassnameHead) {
		this.cssClassnameHead = cssClassnameHead;
	}
	
	public Map<Object, Object> getSidebarHeadMapping() {
		return sidebarHeadMapping;
	}
	
	public void setSidebarHeadMapping(Map<Object, Object> sidebarHeadMapping) {
		this.sidebarHeadMapping = sidebarHeadMapping;
	}

}
